#include "Common/Common.h"
#include "EventReceiver.h"
#include "Renderer.h"
#include "NFData/NetflixDataElement.h"
#include "NFData/DataMgr.h"
#include <iostream>


using namespace irr; 
using namespace core;
using namespace gui; 
using namespace video; 
using namespace scene; 

CEventReceiver::CEventReceiver()
{
	m_bFlagCreate = false;
}

CEventReceiver::~CEventReceiver()
{
	if(m_bFlagCreate)
		Destroy();
}

int CEventReceiver::Create(IrrlichtDevice* ptrDev)
{
	if(m_bFlagCreate)
		return ALREADY_CREATE;

	if(!ptrDev)
		return INVALID_PARAM;

	m_ptrDevice = ptrDev;

	m_bFlagCreate = true;

	return SUCCESS;
}

int CEventReceiver::Destroy()
{
	if(!m_bFlagCreate)
		return NOT_CREATE;

	m_ptrDevice = NULL;

	m_bFlagCreate = false;

	return SUCCESS;
}

bool CEventReceiver::OnEvent(const SEvent& event)
{
	switch(event.EventType)
	{
		case EET_MOUSE_INPUT_EVENT :
			m_HandleMouseEvent(event);
			break;

		case EET_KEY_INPUT_EVENT :
			m_HandleKeyEvent(event);
			break;

		case EET_GUI_EVENT :
			m_HandleGUIEvent(event);
			break;
	}

	return false; 
}

void CEventReceiver::m_HandleMouseEvent(const SEvent& event)
{
	switch(event.MouseInput.Event)
	{
		case EMIE_LMOUSE_PRESSED_DOWN :
			m_LButtonDown(event);
			break;
	}
}

void CEventReceiver::m_HandleKeyEvent(const SEvent& event)
{
	CRenderer*	ptrRenderer = CRenderer::Instance();

	switch(event.KeyInput.Key)
	{
		case KEY_RETURN :
				ptrRenderer->UpdateQuery();
			break;
	}
}

void CEventReceiver::m_HandleGUIEvent(const SEvent& event)
{
	int nId;
	CRenderer* ptrRenderer = CRenderer::Instance();

	switch(event.GUIEvent.EventType)
	{
		case EGET_SCROLL_BAR_CHANGED :
			m_handleScrollBarEvent(event);
			break;
		case EGET_CHECKBOX_CHANGED :
			nId = event.GUIEvent.Caller->getID();
			ptrRenderer->FlipVisible((CATEGORY)nId);
			break;
	}
}

void CEventReceiver::m_handleScrollBarEvent(const SEvent& event) {
	
	CRenderer* ptrRenderer = CRenderer::Instance();
	IGUIScrollBar* barPtr = (IGUIScrollBar*)event.GUIEvent.Caller;

	s32 id = event.GUIEvent.Caller->getID();

	s32 pos = barPtr->getPos();
	double max = barPtr->getMax();
	double percentage = pos/max;
	double minValue = 0;

	if(id == 1) { //Rating filter
		minValue = (int)(percentage*5);
	}
	else if(id == 2) { //Sentiment Filter
		minValue = percentage;
	}
	
	ptrRenderer->FilterNodes(minValue, id);
}

void CEventReceiver::m_LButtonDown(const SEvent& event)
{
	CRenderer*				ptrRender	= CRenderer::Instance();
	ICameraSceneNode*		ptrCamera	= NULL;
	ISceneManager*			ptrSmgr		= NULL;
	IVideoDriver*			ptrDriver	= NULL;
	ISceneNode*				ptrSelNode	= NULL;
	ISceneCollisionManager*	ptrColMgr	= NULL;
	line3d<f32>				ray;
	vector3df				intersection;
	triangle3df				hitTriangle;

	ptrSmgr		= ptrRender->SceneManager();
	ptrDriver	= ptrSmgr->getVideoDriver();
	ptrCamera	= ptrSmgr->getActiveCamera();
	ptrColMgr	= ptrSmgr->getSceneCollisionManager();
	
	ptrSelNode = ptrColMgr->getSceneNodeFromScreenCoordinatesBB(position2di(event.MouseInput.X, event.MouseInput.Y), 0, false);
 
	if(!ptrSelNode->isVisible())
		return ;

	ptrRender->SetSelectedNode(ptrSelNode);
}

//Sets a pointer to the main NetflixData map.
void CEventReceiver::m_setDataPtr(std::unordered_map<irr::scene::ISceneNode*, int>* data) {
	m_dataList = data;
}


